#include <interface\passwordField.h>
#include <interface\gl.h>
#include <interface\window.h>

#include <time.h>

        passwordField::passwordField(): textField(),
                                        crypted( "" )
{
    ELname = "<passwordField>";
}
        passwordField::passwordField(window* win):  textField(win),
                                                    crypted( "" )
{
    ELname = "<passwordField>";
}
        passwordField::~passwordField(){

        }

        void passwordField::display(){
    if( !styleS ) return;
    abstractTextInput::display();
    int pOffX = 0;
    int pOffY = 0;
    getOffset(&pOffX,&pOffY);
    glColor(bkgColFromSS(elm_passwordField));
    glDrawRect(enc,pOffX,pOffY);
    glColor(txtColFromSS(elm_passwordField));
    if( active && lastCursorPos >= (enc.right-enc.left-margin.left-margin.right) ){
        rasterOffset += (enc.right-enc.left-margin.left-margin.right) - lastCursorPos;
    }else if( active && lastCursorPos < (enc.right-enc.left-margin.left-margin.right-5) && rasterOffset != 0 ){
        rasterOffset += (enc.right-enc.left-margin.left-margin.right-5) - lastCursorPos;
        if( rasterOffset > 0 ) rasterOffset = 0;
    }
    glRasterPos(enc.left+margin.left+rasterOffset+pOffX,enc.bottom+1+margin.bottom+pOffY);
    if( active ){
        int cursorRasterPos = enc.left+margin.left+pOffX;
        if( checkClick == -1 ){
            glCurPrint(TFfont,cursor,&cursorRasterPos,crypted.c_str());
            if( (clock()/800)%2 ){
                if( parentWin && parentWin->getInput()->getInserMode() ){
                    glRasterPos(enc.left+margin.left-1+cursorRasterPos+pOffX,enc.bottom+2+margin.bottom+pOffY);
                    glPrint(TFfont,"|");
                }else{
                    glRasterPos(enc.left+margin.left+cursorRasterPos+pOffX,enc.bottom+1+margin.bottom+pOffY);
                    glPrint(TFfont,"_");
                }
            }
        }else{
            glGetCursorPrint(TFfont,checkClick,&cursor,&cursorRasterPos,crypted.c_str());
            checkClick = -1;
            if( (clock()/800)%2 ){
                if( parentWin && parentWin->getInput()->getInserMode() ){
                    glRasterPos(enc.left+margin.left-1+cursorRasterPos+pOffX,enc.bottom+2+margin.bottom+pOffY);
                    glPrint(TFfont,"|");
                }else{
                    glRasterPos(enc.left+margin.left+cursorRasterPos+pOffX,enc.bottom+1+margin.bottom+pOffY);
                    glPrint(TFfont,"_");
                }
            }
        }
        lastCursorPos = cursorRasterPos;
    }else{
        glPrint(TFfont,crypted.c_str());
    }
    glDisable(GL_SCISSOR_TEST);
}
        unsigned char passwordField::eventExec(unsigned char in){
    in = textField::eventExec(in);
    while( content.size() > crypted.size() ){
        crypted += '*';
    }
    while( content.size() < crypted.size() ){
        crypted.erase( crypted.end()-1 );
    }
    return in;
        }
